#!/usr/bin/env node

const http=require('http'),
      fs=require('fs');


function genPage(){
  let html = fs.readFileSync('./chapters.html');
  return html;
}
function genData(){
  let data1=require('./data.js');
  for(let i=0;i<data1.chapterList.length;i++){
    for(let j=0;j<i;j++){
      if(data1.chapterList[j].publishTimer<data1.chapterList[j+1].publishTimer){
        var temp = data1.chapterList[j];
        data1.chapterList[j] = data1.chapterList[j+1];
        data1.chapterList[j+1] = temp;
      }
    }
  }
  console.log(data1.chapterList);
  return JSON.stringify(data1.chapterList);
}

let basecss=fs.readFileSync('./css/base.css');
let stylecss=fs.readFileSync('./css/style.css');
let hline=fs.readFileSync('./images/img/h_line.jpg');
let rline=fs.readFileSync('./images/img/r_line.jpg');
let time=fs.readFileSync('./images/img/time.jpg');
let new1=fs.readFileSync('./images/img/newsbg01.png');
let new3=fs.readFileSync('./images/img/newsbg03.png');
let img40=fs.readFileSync('./images/1442457564979540.jpeg');
let img38=fs.readFileSync('./images/1442201163344838-lp.jpg');
let img84=fs.readFileSync('./images/1442539025939884-lp.jpg');
let img97=fs.readFileSync('./images/1442641275653897-lp.jpg');
let img01=fs.readFileSync('./images/1442642178239101-lp.jpg');
let img85=fs.readFileSync('./images/1447641406565685-lp.jpg');
http.createServer((req,res)=>{
  if(req.url === '/'){
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end(genPage());
  }
  if(req.url==='/getlist'){
    res.end(genData());
  }
  if(req.url === '/css/base.css'){
    res.end(basecss);
  }
  if(req.url==='/css/style.css'){
    res.end(stylecss);
  }
  if(req.url==='/images/img/h_line.jpg'){
    res.end(hline);        
  }
  if(req.url==='/images/img/r_line.jpg'){
    res.end(rline);        
  }
  if(req.url==='/images/1442457564979540.jpeg'){
    res.end(img40);        
  }
  if(req.url==='/images/1442201163344838-lp.jpg'){
    res.end(img38);        
  }
  if(req.url==='/images/1442539025939884-lp.jpg'){
    res.end(img84);        
  }
  if(req.url==='/images/1442642178239101-lp.jpg'){
    res.end(img01);        
  }
  if(req.url==='/images/1442641275653897-lp.jpg'){
    res.end(img97);            
  }
  if(req.url==='/images/1447641406565685-lp.jpg'){
    res.end(img85);             
  }

  if(req.url==='/images/img/newsbg03.png'){
    res.end(new3);  
  }
  if(req.url==='/images/img/newsbg01.png'){
    res.end(new1);        
  }
  if(req.url==='/images/img/time.jpg'){
    res.end(time);
  }
}).listen(8080);
